package ch.escaflowne.mangalist;

import android.app.Activity;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class addVolume extends Activity
{
	Manga manga;
	int _id;
	Toast toast;
	public final static String INTENT_PARAMETER_VOLUME = "VOLUME";
	public final static String INTENT_PARAMETER_ISBN = "ISBN";
	public final static String INTENT_PARAMETER_MANGAID = "MANGAID";
	//public final static String INTENT_PARAMETER_
	
	@Override
	public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	
    	manga = new Manga(getApplicationContext());
    	
    	setContentView(R.layout.addvolume);
    	
    	final Button scanISBN = (Button)findViewById(R.id.btn_scanISBN);
    	final Button addVolume = (Button)findViewById(R.id.btn_addVolume);
    	final CheckBox addSeveral = (CheckBox)findViewById(R.id.cb_addSeveral);
    	
    //	scanISBN.requestFocus();
    	scanISBN.getBackground().setColorFilter(new LightingColorFilter(0xFF00FF00, 0xFF00C000));
    	
    	Bundle extras = getIntent().getExtras();
    	_id = extras.getInt(INTENT_PARAMETER_MANGAID);
    	
    	EditText vol = (EditText)findViewById(R.id.newVolume);
    	vol.setText(String.valueOf(extras.getInt(INTENT_PARAMETER_VOLUME)));
    	
		scanISBN.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				scanBarcode();
			}
		});
		
		addVolume.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//Intent intent = new Intent();
				EditText isbn = (EditText)findViewById(R.id.newISBN);
				EditText newvol = (EditText)findViewById(R.id.newVolume);
				
				if (isbn.getText().length() != 0 && newvol.getText().length() != 0)
				{
					/*
					intent.putExtra(INTENT_PARAMETER_ISBN, isbn.getText().toString());
					intent.putExtra(INTENT_PARAMETER_VOLUME, Integer.parseInt(newvol.getText().toString()));
					setResult(RESULT_OK, intent);
					*/
					
					if (!manga.addVolume(isbn.getText().toString(), _id, Integer.parseInt(newvol.getText().toString())))
					{
						// error while adding to database
						if (manga.findMangabyISBN(isbn.getText().toString()) != _id)
						{
							// isbn already in use by another series, change to current series
							manga.getWritableDatabase().rawQuery("UPDATE volumes SET mangaid='" + _id +"' WHERE _id = '" + isbn.getText().toString() + "'", null);
							manga.getWritableDatabase().rawQuery("UPDATE volumes SET volume='" + Integer.parseInt(newvol.getText().toString()) +"' WHERE _id = '" + isbn.getText().toString() + "'", null);
							toast = Toast.makeText(getApplicationContext(), "ISBN was already in Database, changed entry to fit current selection", Toast.LENGTH_LONG);
							toast.show();
						}
					//	toast = Toast.makeText(getApplicationContext(), "could not add volume", Toast.LENGTH_SHORT);
					//	toast.show();
					}

					if (!addSeveral.isChecked())
					{	//Return after a Job well done
						finish();
					}
					else
					{	//Add another Manga
						newvol.setText("" + (Integer.parseInt(newvol.getText().toString()) + 1));	//Increment volume counter
						isbn.setText("");
						
						toast = Toast.makeText(getApplicationContext(), "Scan volume" + newvol.getText().toString(), Toast.LENGTH_SHORT);
						toast.show();

						//enter scanning mode for maximum convinience (hell yeah!)
						scanBarcode();
					}
				}
			}
		});
		
		addSeveral.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
			//	EditText et = (EditText)findViewById(R.id.newISBN);
			//	et.setEnabled(!addSeveral.isChecked());
			}
		});
    	
    }

	private static final int INTENT_NR_SCAN_BARCODE = 0;
	
	private void scanBarcode()
	{
		Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		intent.setPackage("com.google.zxing.client.android");
		intent.putExtra("SCAN_MODE", "PRODUCT_CODE");
		startActivityForResult(intent, INTENT_NR_SCAN_BARCODE);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		Toast toast;
		switch (requestCode)
		{
		case INTENT_NR_SCAN_BARCODE:	// Returning from ScanBarcode()
			switch (resultCode)
			{
			case RESULT_OK:
				String isbn = intent.getStringExtra("SCAN_RESULT");
				
				if (!isbn.startsWith("978"))
				{
					toast = Toast.makeText(getApplicationContext(), "Scan returned an ivalid ISBN number. ISBN numbers start with 978", Toast.LENGTH_SHORT);
					toast.show();
					break;
				}
				
				EditText et_isbn = (EditText)findViewById(R.id.newISBN);
				
				et_isbn.setText(isbn);
				
				//Set focus on "add" button, this seems to cause a problem where one has to press the button twice..
			//	((Button)findViewById(R.id.btn_addVolume)).requestFocus();
				
				break;
				
			case RESULT_CANCELED:
				// Handle cancel
				toast = Toast.makeText(getApplicationContext(), "Scan aborted", Toast.LENGTH_SHORT);
				toast.show();
				break;
			}
			break;
		}
	}
}
